<?php

/**
 * Created by PhpStorm.
 * User: moting
 * Date: 2017/6/12
 * Time: 15:48
 */
class Controller_Manager extends Controller
{
    //默认主界面
    public function action_index(){
        return View::forge('manager/index');
    }
    //默认的操作界面
    public function action_main(){
        return View::forge('manager/main');
    }
    //默认的数据界面
    public function action_datalist($move_type){
        $view = View::forge('manager/datalist');
        $view->move_type = $move_type;
        return $view;
    }
    //默认的编辑界面
    public function action_edit($current_id){
        $view = View::forge('manager/edit');
        Session::set('current_id',$current_id);
        $view->handle_type = "edit";
        return $view;
    }
    //默认的添加页面
    public function action_create(){
        $view = View::forge('manager/edit');
        $view->handle_type = "add";
        return $view;
    }
    //默认上传视频页面
    public function action_upvideo($currentId){
        $view = View::forge('manager/upvideo');
        $view->current_movie_id = $currentId;
        return $view;
    }

//    public function get_checklogin(){
//        $obj = Auth::create_user('yujian','yujian','yujian@yu.com',5);
//        //$obj = \Auth\Model\Auth_User::find(124);
//        var_dump($obj);die;
//    }
    //通过POST方式，请求方可检测账号是否存在或校验账号密码
    public function post_checklogin(){
        $handdle_type = Input::post('type');
        switch ($handdle_type) {
            case 'checkUser':
                //获取数据
                $username = Input::post('username');
                $post = DB::query('SELECT count(*) as num FROM users WHERE username = \''.$username.'\' OR email =\''.$username.'\'')->execute()->as_array();
                if ($post[0]['num']>0){
                    echo 'success';die;
                }
                break;
            case 'checkLogin':
                $username = Input::post('username');
                $userpassword = Input::post('password');
                if(\Auth\Auth::login($username,$userpassword)){
                    echo "success";die;
                }
                break;
            case 'logout':
                if(Auth::logout()){
                   echo "success";die;
                }
                break;
            default:
                ;
        }
        echo 'error';die;
    }
    //通过POST方式，请求页面的数据，比如电影、电视剧。。。
    public function post_pagedata(){
        switch (Input::post('type'))
        {
            case 'page':
                $data = Model_Movie::find('all',array('where' => array('movie_type' => Input::post('movie_type'))));
                $result_array = array();
                $base_uri = \Fuel\Core\Uri::base();
                $i=0;
                foreach ($data as $data_item)
                {
                    $result_array[$i] = array(
                        'id'                => $data_item->id,
                        'movie_name'        => $data_item->movie_name,
                        'update_time'       => $data_item->update_time,
                        'director_name'     => $data_item->director_name,
                        'actor_name'        => $data_item->actor_name,
                        'movie_type'        => $data_item->movie_type,
                        'state_name'        => $data_item->state_name,
                        'grade_num'         => $data_item->grade_num,
                        'picture_src_path'  => $base_uri . $data_item->picture_src_path,
                        'movie_detail'      => $data_item->movie_detail,
                        'movie_path'        => $data_item->movie_path,
                        'create_time'       => $data_item->create_time,
                        'movie_sign'        => $data_item->movie_sign,
                        'delete_sign'       => $data_item->delete_sign,
                        'display_sign'       => $data_item->display_sign,
                    );
                    $i++;
                }
                echo json_encode($result_array);die;
                break;
            case 'getdata':
                $base_uri = \Fuel\Core\Uri::base();
                $data = Model_Movie::find(Session::get('current_id'));
                $result_array = array(
                    'id'                => $data->id,
                    'movie_name'        => $data->movie_name,
                    'update_time'       => $data->update_time,
                    'director_name'     => $data->director_name,
                    'actor_name'        => $data->actor_name,
                    'movie_type'        => $data->movie_type,
                    'state_name'        => $data->state_name,
                    'grade_num'         => $data->grade_num,
                    'picture_src_path'  => $base_uri . $data->picture_src_path,
                    'movie_detail'      => $data->movie_detail,
                    'movie_path'        => $data->movie_path,
                    'movie_sign'        => $data->movie_sign,
                    'create_time'       => $data->create_time,
                    'delete_sign'       => $data->delete_sign,
                    'display_sign'       => $data->display_sign,
                );
                echo json_encode($result_array);die;
            default :
                echo 'error';die;
        }
    }
    //通过POST方式，往数据库插入数据
    public function post_insert(){
        //print_r(Date::time()->format("%Y-%m-%d %H:%M:%S"));die;
        //获取数据
        $movie_data = Model_Movie::forge(
            array(
                'movie_name'        => Input::post('movie_name'),
                'update_time'       => Input::post('update_time'),
                'director_name'     => Input::post('director_name'),
                'actor_name'        => Input::post('actor_name'),
                'movie_type'        => Input::post('movie_type'),
                'state_name'        => Input::post('state_name'),
                'grade_num'         => Input::post('grade_num'),
                'movie_detail'      => Input::post('movie_detail'),
                'picture_src_path'  => strstr(Input::post('picture_src_path'),'assets'),
                'create_time'       => Input::post('create_time',date("Y-m-d H:i:s")),
                'delete_sign'       => Input::post('delete_sign',0),
                'display_sign'      => Input::post('display_sign','checked'),
            )
        );

        if($movie_data and $movie_data->save())
        {
            echo "success";die;
        }
        else
        {
            echo "error";die;
        }
    }
    //通过POST方式，往数据库更新数据
    public function post_update(){
        //获取数据
        $movie_data = Model_Movie::find(
            Session::get('current_id')
        );
        $movie_data->movie_name         = Input::post('movie_name');
        $movie_data->update_time        = Input::post('update_time');
        $movie_data->director_name      = Input::post('director_name');
        $movie_data->actor_name         = Input::post('actor_name');
        $movie_data->movie_type         = Input::post('movie_type');
        $movie_data->state_name         = Input::post('state_name');
        $movie_data->grade_num          = Input::post('grade_num');
        $movie_data->movie_detail       = Input::post('movie_detail');
        $movie_data->picture_src_path   = strstr(Input::post('picture_src_path'),'assets');
        $movie_data->create_time        = Input::post('create_time',date("Y-m-d H:i:s"));
        $movie_data->delete_sign        = Input::post('delete_sign',0);
        //print_r($movie_data);die;
        if($movie_data->save())
        {
            echo "success";die;
        }
        else
        {
            echo "error";die;
        }
    }
    //通过POST方式，更新视频是否可显示的状态
    public function post_display(){
        //获取数据
        $movie_data = Model_Movie::find(
            Input::post('current_id')
        );
        $movie_data->display_sign   = Input::post('display_sign');
        //var_dump($movie_data->display_sign);die;
        //print_r($movie_data);die;
        if($movie_data->save())
        {
            echo "success";die;
        }
        else
        {
            echo "error";die;
        }
    }
    //通过POST方式，删除对应的数据
    public function post_delete()
    {
        if ($post = Model_Movie::find(Input::post('current_id')))
        {
            $post->delete();
            echo "success";die;
        }
        else
        {
            echo "error";die;
        }
    }
    //通过POST方式，上传图片-保存图片-返回图片路径和名称
    public function post_upload(){
        // default img
        $result_image = Asset::get_file("default_upload.png",'img');
        // Custom configuration for this upload
        $config = array(
            'path' => DOCROOT.'assets/files',
            'randomize' => true,
            'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
        );

        // process the uploaded files in $_FILES
        Upload::process($config);

        // if there are any valid files
        if (Upload::is_valid())
        {
            // save them according to the config
            Upload::save();

            // call a model method to update the database
            // print_r(Upload::get_files());die;
            $data = Upload::get_files(0);

            Image::load($data['saved_to'].$data['saved_as'], false, $data['extension'])
                ->resize(190, 270, true, true)
                ->save($data['saved_to'].$data['saved_as']);

            $result_image = Asset::get_file($data['saved_as'],'files');
        }

        // and process any errors
        foreach (Upload::get_errors() as $file)
        {
            // $file is an array with all file information,
            // $file['errors'] contains an array of all error occurred
            // each array element is an an array containing 'error' and 'message'
        }
        $get_result = array(
            'code'  => 0,
            'mag'   => "",
            'url'     => $result_image
        );
        echo json_encode($get_result);
    }
    //通过POST方式，上传视频文件-返回视频的相对目录
    public function post_uploadvideo()
    {
        // default img
        $result_video ="";
        // Custom configuration for this upload
        $config = array(
            'path' => DOCROOT.'assets/video',
            'randomize' => true,
            'ext_whitelist' => array('mp4'),
        );

        // process the uploaded files in $_FILES
        Upload::process($config);

        // if there are any valid files
        if (Upload::is_valid())
        {
            // save them according to the config
            Upload::save();

            // call a model method to update the database
            // print_r(Upload::get_files());die;
            $data = Upload::get_files(0);

            $result_video = Asset::get_file($data['saved_as'],'video');
        }

        $get_result = array(
            'code'  => 0,
            'mag'   => "",
            'url'     => $result_video
        );
        echo json_encode($get_result);
    }
    //通过POST方式，保存视频上传的数据
    public function post_savevideo(){
        //获取数据
        $movie_data = Model_Movie::find(
            Input::post('id')
        );

        $movie_data->movie_path     = strstr(Input::post('movie_path'),'assets');
        $movie_data->movie_sign     = Input::post('movie_sign');

        //print_r($movie_data);die;
        if($movie_data->save())
        {
            echo "success";die;
        }
        else
        {
            echo "error";die;
        }
    }

    public function get_test(){
        //test --- baseUrl
        //echo \Fuel\Core\Uri::base();//http://www.mywork.com/web/YuJian/public/


        //$current_path = substr($current_path,);
        //test ---- 截取字符串
        //echo strstr($current_path,'/assets');
    }

}